<?php

namespace App\Utils;

class NumberTrans
{

    protected static string $sourceStr = '123456789ABCDEFGHIJKLMNPQRSTUVWXYZ';

    public static function encode(int $num)
    {
        $code = '';
        while($num)
        {
            $to = strlen(self::$sourceStr);
            $mod = $num % $to;
            $num = ($num - $mod) / $to;
            $code = self::$sourceStr[$mod].$code;
        }
        return $code;
    }

    public static function decode(string $str)
    {
        $num = 0;
        $base = strlen(self::$sourceStr);
        $len = strlen($str);
        for ($i=0;$i<$len;$i++){
            $d = strpos(self::$sourceStr,$str[$i]);
            if ($d===false){
                return false;
            }
            $num += $d * pow($base, $len-$i-1);
        }
        return $num;
    }
}